package com.zhuojy.equity.mall.admin.service.AttendanceServices;
/**
 * @className: ReplacementCardService
 * @author: Csh
 * @date: 2024/3/19 19:47
 * @Version: 1.0
 * @description:
 */


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuojy.equity.mall.admin.common.entity.*;
import com.zhuojy.equity.mall.admin.common.entity.TUser;
import com.zhuojy.equity.mall.admin.common.pojo.JsonResult;
import com.zhuojy.equity.mall.admin.common.pojo.TreeEntity;
import com.zhuojy.equity.mall.admin.dal.mapper.AttendanceMappers.AttendanceGroupMapper;
import com.zhuojy.equity.mall.admin.dal.mapper.AttendanceMappers.ReplacementCardMapper;
import com.zhuojy.equity.mall.admin.dal.mapper.DemoMapper;
import com.zhuojy.equity.mall.admin.dal.mapper.TPositionMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
@RequiredArgsConstructor
public class ReplacementCardService extends ServiceImpl<ReplacementCardMapper, ReplacementCard> {
//  获取用户信息
    @Autowired
    private DemoMapper demoMapper;
    @Autowired
    private TPositionMapper tPositionMapper;

    @Autowired
    private AttendanceGroupMapper attendanceGroupMapper;
    public JsonResult findUser() {
        for (TUser tUser : demoMapper.selectList(null)) {
            System.out.println(tUser);
        }

        return JsonResult.success("成功");
    }

    public List<TreeEntity> findTreeEntity() {
        List<AttendanceGroup> attendanceGroups = attendanceGroupMapper.selectList(null);
        ArrayList<TreeEntity> list = new ArrayList<>();
        for (AttendanceGroup attendanceGroup : attendanceGroups) {
            TreeEntity treeEntity = new TreeEntity();
            treeEntity.setId(attendanceGroup.getId());
            treeEntity.setLabel(attendanceGroup.getAttendanceName());
            list.add(treeEntity);
        }
        return list;
    }

    public JsonResult findDept() {
        List<TPosition> tPositions = tPositionMapper.selectList(null);
        List<TUser> tUsers = demoMapper.selectList(null);
        ArrayList<TUser> list = new ArrayList<>();
        for (TUser tUser : tUsers) {
            for (TPosition tPosition : tPositions) {
                if(tPosition.getId() == tUser.getPositionId().longValue()){
                    tUser.setTPosition(tPosition);
                    list.add(tUser);
                }
            }
        }
        int count = 0;
        HashSet<String> tPosit = new HashSet<>();
        List<Depts> myP = new ArrayList<>();
        for (TPosition tPosition : tPositions) {
            tPosit.add(tPosition.getDeptName());
        }
        for (String deptName : tPosit) {
            count = 0;
            for (TUser user : list) {
                if(deptName.equals(user.getTPosition().getDeptName())) {
                    count++;
                }
            }
            myP.add(new Depts(deptName, (long) count));
        }
        return JsonResult.success("成功",myP);
    }

    public JsonResult findUserAndDept(String name) {
        List<TPosition> tPositions = tPositionMapper.selectList(null);
        List<TUser> tUsers = demoMapper.selectList(null);
        ArrayList<TUser> list = new ArrayList<>();
        for (TUser tUser : tUsers) {
            for (TPosition tPosition : tPositions) {
                if(tPosition.getId()==tUser.getPositionId().longValue()){
                    if(tPosition.getDeptName().equals(name)){
                        tUser.setTPosition(tPosition);
                        list.add(tUser);
                    }
                }
            }
        }
        return JsonResult.success("成功",list);

    }

    public int addReplacementCard(ReplacementCard replacementCard) {
        replacementCard.setCreatedAt(new Date());
        return this.baseMapper.insert(replacementCard);
    }
    public JsonResult findAllReplacementCard(String rulesName, Integer pageNum, Integer pageSize) {
        QueryWrapper<ReplacementCard> qw = new QueryWrapper<>();
        qw.like("rules_name",rulesName);
        qw.ne("replacement_card_status",0);
        Page<ReplacementCard> page = new Page<>(pageNum,pageSize);
        IPage<ReplacementCard> replacementCardPage = this.baseMapper.selectPage(page, qw);
        ArrayList<ReplacementCard> list = new ArrayList<>();
        for (ReplacementCard record : replacementCardPage.getRecords()) {
            for (TreeEntity treeEntity : findTreeEntity()) {
                if(record.getReplacementApplyId() == Math.toIntExact(treeEntity.getId())){
                    record.setReplacementApply(treeEntity);
                }
                list.add(record);

            }
        }

        return JsonResult.success("成功",list,replacementCardPage.getTotal());
    }

    public int upAndDeleteReplacementCard(ReplacementCard replacementCard) {
        return this.baseMapper.updateById(replacementCard);
    }

    public ReplacementCard findReplacementCard(Long id) {
        return this.baseMapper.selectById(id);
    }

    public int upReplacementCard(ReplacementCard replacementCard) {
        replacementCard.setUpdateAt(new Date());
        return this.baseMapper.updateById(replacementCard);
    }
}
